Imports System
Imports System.IO
Imports Microsoft.VisualBasic

Namespace Logger
    ''' <summary>
    ''' class to the logs
    ''' </summary>
    ''' <remarks></remarks>
    Public Class LogFile
#Region "Private Methods"
        Private m_strFileName As String
        Private m_LogWriter As StreamWriter
#End Region

#Region "Constructor"
        Public Sub New(ByVal strFileName As String)
            m_strFileName = strFileName
        End Sub
#End Region
        ''' <summary>
        ''' make a new logfile
        ''' </summary>
        ''' <param name="sEvent">event</param>
        ''' <param name="iEventLevel">level of the event</param>
        ''' <param name="bClose">select if the log must be close or not</param>
        ''' <remarks></remarks>
#Region "Public Methods"
        Public Sub WriteLog(ByVal sEvent As String, Optional ByVal iEventLevel As Long = 1, Optional ByVal bClose As Boolean = True)
            Dim sLine As String

            If Not Directory.Exists(Path.GetDirectoryName(m_strFileName)) Then
                Directory.CreateDirectory(Path.GetDirectoryName(m_strFileName))
            End If

            sLine = Format(Now, "MM/dd/yyyy HH:mm:ss tt - ") & sEvent
            m_LogWriter = File.AppendText(m_strFileName)
            m_LogWriter.WriteLine(sLine)

            If bClose Then
                m_LogWriter.Close()
            End If
        End Sub
        ''' <summary>
        ''' overwrite the logfile
        ''' </summary>
        ''' <param name="sLine">text that going to overwirte</param>
        ''' <remarks></remarks>
        Public Sub OverWrite(ByVal sLine As String)
            If Not Directory.Exists(Path.GetDirectoryName(m_strFileName)) Then
                Directory.CreateDirectory(Path.GetDirectoryName(m_strFileName))
            End If
            m_LogWriter = File.CreateText(m_strFileName)

            m_LogWriter.Write(sLine)
            m_LogWriter.Close()
        End Sub
        ''' <summary>
        ''' write over the logfile
        ''' </summary>
        ''' <param name="sLine">text that going to write</param>
        ''' <remarks></remarks>
        Public Sub Write(ByVal sLine As String)
            If Not File.Exists(m_strFileName) Then
                If Not Directory.Exists(Path.GetDirectoryName(m_strFileName)) Then
                    Directory.CreateDirectory(Path.GetDirectoryName(m_strFileName))
                End If
                m_LogWriter = File.CreateText(m_strFileName)
            Else
                m_LogWriter = File.AppendText(m_strFileName)
            End If

            m_LogWriter.Write(sLine)
            m_LogWriter.Close()
        End Sub
        ''' <summary>
        ''' write inside of starter file
        ''' </summary>
        ''' <param name="sLine">text what is goint to write in the starter file</param>
        ''' <remarks></remarks>
        Public Sub WriteOnStartFile(ByVal sLine As String)
            If File.Exists(m_strFileName) Then
                m_LogWriter = File.CreateText(m_strFileName)
                m_LogWriter.Write(sLine)
                m_LogWriter.Close()
            End If
        End Sub
        ''' <summary>
        ''' close the log
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub CloseLog()
            m_LogWriter.Close()
        End Sub
#End Region
    End Class
End Namespace